﻿#include "systemsetting_3.h"
#include "ui_systemsetting_3.h"

SystemSetting_3::SystemSetting_3(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SystemSetting_3)
{
    ui->setupUi(this);
    time = new QLabel(ui->frameBottom);
    time->setGeometry(10, 10, 200, 40);
    time->setStyleSheet("QLabel{font: 20px; color: rgb(25, 25, 112);}");
    signal = new QLabel(ui->frameBottom);
    signal->setGeometry(700, 18, 41, 22);
    signal->setStyleSheet("QLabel{background-color:transparent;}");

    QDir dir(IMAGE_PATH);
    if(dir.exists())
    {
        ui->frameTop->setStyleSheet(LOGO_STY);
    }

    ui->labelSystem3Hint->hide();

    _V_I_Model = new QStandardItemModel(this);
    _V_I_Model->setColumnCount(2);
    _V_I_Model->setRowCount(1);
    _V_I_mapper = new QDataWidgetMapper(this);
    _V_I_mapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
    QStandardItem* root = _V_I_Model->invisibleRootItem();
    QStandardItem* maxV = new QStandardItem(readFile(QString(CHARGE_CONF), QString("MaxV")));
    QStandardItem* maxI = new QStandardItem(readFile(QString(CHARGE_CONF), QString("MaxI")));
    root->setChild(0,0,maxV);
    root->setChild(0,1,maxI);
    _V_I_mapper->setModel(_V_I_Model);
    _V_I_mapper->addMapping(ui->m_editMaxV, 0);
    _V_I_mapper->addMapping(ui->m_editMaxI, 1);
    _V_I_mapper->toFirst();

    connect(stackLayout,SIGNAL(currentChanged(int)),this,SLOT(show_V_I(int)));
    keyBoardConnect();
}

void SystemSetting_3::show_V_I(int current)
{
    if(current == SystemSetting3Page)
    {
        index = _V_I_Model->index(0, 0);
        _V_I_Model->setData(index,readFile(QString(CHARGE_CONF), QString("MaxV")),Qt::EditRole);
        index = _V_I_Model->index(0, 1);
        _V_I_Model->setData(index,readFile(QString(CHARGE_CONF), QString("MaxI")),Qt::EditRole);
    }
}
void SystemSetting_3::on_buttonBack_clicked()
{
    stackLayout->setCurrentIndex(ChooseGunPage);
}

void SystemSetting_3::on_buttonPrev_clicked()
{
    stackLayout->setCurrentIndex(SystemSetting2Page);
}

void SystemSetting_3::on_buttonNext_clicked()
{
    stackLayout->setCurrentIndex(SystemSetting4Page);
}

void SystemSetting_3::on_buttonSave_clicked()
{

    bool ret0 = writeFile(QString(CHARGE_CONF), QString("MaxI"), ui->m_editMaxI->text());
    if(ret0)
    {
       chargingData->cmd1.flag_change = 0;
    }
    bool ret1 = writeFile(QString(CHARGE_CONF), QString("MaxV"), ui->m_editMaxV->text());
    if(ret1)
    {
       chargingData->cmd1.flag_change = 0;
    }
    if(ret0 && ret1)
    {
        ui->labelSystem3Hint->setText(tr("设置成功"));
        ui->labelSystem3Hint->show();
        QTimer::singleShot(3000,ui->labelSystem3Hint,SLOT(hide()));//显示三秒后隐藏
    }
    else
    {
        ui->labelSystem3Hint->setText(tr("设置失败"));
        ui->labelSystem3Hint->show();
        QTimer::singleShot(3000,ui->labelSystem3Hint,SLOT(hide()));//显示三秒后隐藏
    }
    /*待完成*/
}

void SystemSetting_3::keyHandle(QPushButton* btn, QLineEdit* lineEdit)
{
    if( btn->text() == tr("退格") )
    {
        lineEdit->backspace();
    }
    else if( btn->text() == tr("清除"))
    {
        lineEdit->clear();
    }
    else
    {
        lineEdit->insert(btn->text());
    }
}

void SystemSetting_3::on_Keyboard_clicked()
{
    QPushButton* btn =  static_cast<QPushButton*>(sender());

    QLineEdit* focus = (QLineEdit* )QApplication::focusWidget();

    keyHandle(btn, focus);
}

void SystemSetting_3::keyBoardConnect()
{
    connect(ui->One, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Tow, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Three, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Four, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Five, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Six, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Seven, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Eight, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Nine, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Zero, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Remove, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->RemoveAll, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
//    connect(ui->Char1, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
//    connect(ui->Char2, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Char3, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
}

SystemSetting_3::~SystemSetting_3()
{
    delete ui;
}
